﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Business.Fa;
using ESOL.Common;

namespace ESOL.WinUI.Fa
{
    public partial class frmMaintainAssetType : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        public FaAssetsTypeEntity SelectedAssetType { get; set; }
        #endregion

        #region Construction
        public frmMaintainAssetType()
        {
            InitializeComponent();
        }
        #endregion

        #region Function
        private void SetField()
        {
            if (SelectedAssetType == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(SelectedAssetType.ParentId) == false)
            {
                FaAssetsTypeEntity parent = FaAssetsTypeBs.GetFaAssetsTypeByID(SelectedAssetType.ParentId);
                txtParentType.Text = parent.AssetTypeName;
                txtParentType.Tag = parent;
            }
            txtTypeCode.Text = SelectedAssetType.AssetTypeCode;
            txtTypeName.Text = SelectedAssetType.AssetTypeName;
            txtDescription.Text = SelectedAssetType.Description;

        }
        private void SetEntity()
        {
            if (SelectedAssetType == null)
            {
                SelectedAssetType = new FaAssetsTypeEntity();
                SelectedAssetType.AssetTypeId = Guid.NewGuid().ToString();
            }
            SelectedAssetType.ParentId = null;
            if (txtParentType.Tag != null && txtParentType.Tag.GetType() == typeof(FaAssetsTypeEntity))
            {
                FaAssetsTypeEntity parent = (FaAssetsTypeEntity)txtParentType.Tag;
                SelectedAssetType.ParentId = parent.AssetTypeId;
            }
            SelectedAssetType.AssetTypeCode = txtTypeCode.Text;
            SelectedAssetType.AssetTypeName = txtTypeName.Text;
            SelectedAssetType.Description = txtDescription.Text;
        }
        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtTypeCode.Text))
            {
                MessageBox.Show("类别编码不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtTypeName.Text))
            {
                MessageBox.Show("类别名称不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }
        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                SetEntity();
                if (ThisActionType == ActionType.New)
                {
                    if (FaAssetsTypeBs.CheckTypeCode(SelectedAssetType.AssetTypeCode, "") == false)
                    {
                        MessageBox.Show("编码" + SelectedAssetType.AssetTypeCode + "已被占用，请重新输入一个！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                    bool isSuccessful = FaAssetsTypeBs.InsertFaAssetsType(SelectedAssetType);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    if (FaAssetsTypeBs.CheckTypeCode(SelectedAssetType.AssetTypeCode, SelectedAssetType.AssetTypeId) == false)
                    {
                        MessageBox.Show("编码" + SelectedAssetType.AssetTypeCode + "已被占用，请重新输入一个！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                    bool isSuccessful = FaAssetsTypeBs.UpdateFaAssetsType(SelectedAssetType);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region Events
        private void frmMaintainAssetType_Load(object sender, EventArgs e)
        {
            SetField();
        }
        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        private void txtParentType_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainAssetTypeQuery frm = new frmMaintainAssetTypeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedAssetType != null)
            {
                txtParentType.Text = frm.SelectedAssetType.AssetTypeName + " (" + frm.SelectedAssetType.AssetTypeCode + ")";
                txtParentType.Tag = frm.SelectedAssetType;
            }
            else
            {
                txtParentType.Text = "";
                txtParentType.Tag = null;
            }
        }
        private void txtTypeCode_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtTypeCode.Text) == false && txtTypeCode.Text.Length != 3)
            {
                MessageBox.Show("类型编码长度必须为3", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtTypeCode.Focus();
            }
        }
        #endregion


    }
}